package com.bsj.power.pls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.dto.locationDevice.LocationDeviceLogSearchDTO;
import com.bsj.power.common.def.entity.locationDevice.LocationDeviceLog;
import com.bsj.power.common.def.vo.pls.LocationDeviceLogVO;
import com.bsj.power.common.def.vo.pls.excel.LocationDeviceLogExcelVO;
import com.bsj.power.common.mapper.LocationDeviceLogMapper;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.pls.service.LocationDeviceLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description 定位设备日志业务处理具体实现
 * @time 2024/12/6 12:03
 */
@Service
public class LocationDeviceLogServiceImpl implements LocationDeviceLogService {

    @Resource
    private LocationDeviceLogMapper locationDeviceLogMapper;

    /**
     * 定位设备日志分页
     *
     * @param locationDeviceLogSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.pls.LocationDeviceLogVO>> 定位设备日志信息
     * @author ljx
     * @time 2024/12/6 14:01
     */
    @Override
    public JsonResult<List<LocationDeviceLogVO>> pageLocationDeviceLog(LocationDeviceLogSearchDTO locationDeviceLogSearchDTO) {
        List<LocationDeviceLogVO> locationDeviceLogVOS = new ArrayList<>();
        IPage<LocationDeviceLog> page = new Page<>(locationDeviceLogSearchDTO.getPageNumber(), locationDeviceLogSearchDTO.getPageSize());
        QueryWrapper<LocationDeviceLog> queryWrapper = new QueryWrapper<>();
        Integer type = locationDeviceLogSearchDTO.getType();
        String deviceName = locationDeviceLogSearchDTO.getDeviceName();
        String startTime = locationDeviceLogSearchDTO.getStartTime();
        String endTime = locationDeviceLogSearchDTO.getEndTime();
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            queryWrapper.lambda().between(LocationDeviceLog::getCreateTime, startTime, endTime);
        }
        if (type != null) {
            queryWrapper.lambda().eq(LocationDeviceLog::getType, type);
        }
        if (StringUtils.isNotBlank(deviceName)) {
            queryWrapper.lambda().eq(LocationDeviceLog::getDeviceName, deviceName);
        }
        queryWrapper.lambda().orderByDesc(LocationDeviceLog::getCreateTime);
        IPage<LocationDeviceLog> iPage = locationDeviceLogMapper.selectPage(page, queryWrapper);
        long total = iPage.getTotal();
        List<LocationDeviceLog> locationDeviceLogs = iPage.getRecords();
        if (total == NumericalConstants.ZERO || CollectionUtils.isEmpty(locationDeviceLogs)) {
            return JsonResult.success(locationDeviceLogVOS, total);
        }
        Date date = new Date();
        locationDeviceLogVOS = locationDeviceLogs.stream().map(locationDeviceLog -> {
            LocationDeviceLogVO locationDeviceLogVO = new LocationDeviceLogVO();
            BeanUtils.copyProperties(locationDeviceLog, locationDeviceLogVO);
            if (locationDeviceLogVO.getUpdateTime() == null) {
                locationDeviceLogVO.setUpdateTime(date);
            }
            return locationDeviceLogVO;
        }).collect(Collectors.toList());
        return JsonResult.success(locationDeviceLogVOS, total);
    }

    /**
     * 获取定位设备日志导出数据
     *
     * @param locationDeviceLogSearchDTO 定位设备日志搜索参数
     * @return java.util.List<com.bsj.power.common.def.vo.pls.excel.LocationDeviceLogExcelVO> 定位设备日志信息
     * @author ljx
     * @time 2024/12/6 14:22
     */
    @Override
    public List<LocationDeviceLogExcelVO> getExportData(LocationDeviceLogSearchDTO locationDeviceLogSearchDTO) {
        List<LocationDeviceLogExcelVO> locationDeviceLogExcelVOS = new ArrayList<>();
        QueryWrapper<LocationDeviceLog> queryWrapper = new QueryWrapper<>();
        Integer type = locationDeviceLogSearchDTO.getType();
        String deviceName = locationDeviceLogSearchDTO.getDeviceName();
        String startTime = locationDeviceLogSearchDTO.getStartTime();
        String endTime = locationDeviceLogSearchDTO.getEndTime();
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            queryWrapper.lambda().between(LocationDeviceLog::getCreateTime, startTime, endTime);
        }
        if (type != null) {
            queryWrapper.lambda().eq(LocationDeviceLog::getType, type);
        }
        if (StringUtils.isNotBlank(deviceName)) {
            queryWrapper.lambda().eq(LocationDeviceLog::getDeviceName, deviceName);
        }
        queryWrapper.lambda().orderByDesc(LocationDeviceLog::getCreateTime);
        List<LocationDeviceLog> locationDeviceLogs = locationDeviceLogMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(locationDeviceLogs)) {
            return locationDeviceLogExcelVOS;
        }
        locationDeviceLogExcelVOS = locationDeviceLogs.stream().map(locationDeviceLog -> {
            LocationDeviceLogExcelVO locationDeviceLogExcelVO = new LocationDeviceLogExcelVO();
            BeanUtils.copyProperties(locationDeviceLog, locationDeviceLogExcelVO);
            locationDeviceLogExcelVO.setType(locationDeviceLog.getType().equals(NumericalConstants.ONE) ? "离线" : "上线");
            locationDeviceLogExcelVO.setCreateTime(DateUtils.dateToStr(locationDeviceLog.getCreateTime()));
            locationDeviceLogExcelVO.setUpdateTime(DateUtils.dateToStr(locationDeviceLog.getUpdateTime()));
            return locationDeviceLogExcelVO;
        }).collect(Collectors.toList());
        return locationDeviceLogExcelVOS;
    }
}
